package com.base.stack;

import java.util.HashMap;
import java.util.Stack;

public class IsValid {
    public static boolean isValid(String s){
        if (s==null||s.length()==0){
            return false;
        }
        Stack<Character> stack =new Stack<>();
        HashMap<Character, Character> map = new HashMap<>(3);
        map.put('(',')');
        map.put('[',']');
        map.put('{','}');
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
                if (map.containsKey(chars[i])){
                    stack.add(chars[i]);
                }else {

                    if (stack.isEmpty()||map.get(stack.pop()) != chars[i]){
                        return false;
                    }
                }
        }
        if (stack.isEmpty()){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(isValid("["));
    }
}
